package auctionhausen



import grails.test.mixin.*

import org.junit.*

/**
 * See the API for {@link grails.test.mixin.services.ServiceUnitTestMixin} for usage instructions
 */
@TestFor(BidService)
@Mock([Bid, Customer, Listing])
class BidServiceTests {

	void testCreateBid() {
		service.listingService = [getListing: {it -> return true}, isListingExpired: {return false}]
		def seller = new Customer(email:'mitch@tes.edu', password:'PASSWOR')
		seller.save(failOnError: true)
		def bidder = new Customer(email:'h@tes.edu', password:'PASSWOR')
		bidder.save(failOnError: true)
		def listing = new Listing(
				name: 'Roger Rabbit Action Figure',
				endDate: new Date() + 1,
				startingPrice: 35.50,
				description: 'Mint condition! **Like New**',
				winner: null,
				seller: seller)
		listing.save(failOnError: true)

		def params  = [:]
		params["amount"] = 36.00
		params["bidder.id"] = bidder.id
		params["listing.id"] = listing.id
		params["listing"] = ["id":listing.id]
		
		def bid = service.createBid(params)
		assertFalse bid.hasErrors()
		assert Bid.count() == 1
	}

	void testCreateBidFails_InvalidListingAndBidder() {
		service.listingService = [getListing: {it -> return true}, isListingExpired: {return false}]
		def seller = new Customer(email:'mitch@tes.edu', password:'PASSWOR')
		seller.save(failOnError: true)
		def listing = new Listing(
				name: 'Roger Rabbit Action Figure',
				endDate: new Date() + 1,
				startingPrice: 35.50,
				description: 'Mint condition! **Like New**',
				winner: null,
				seller: seller)
		listing.save(failOnError: true)

		def params = [:]
		params["amount"] = 36.00
		def bid = service.createBid(params)
		assertTrue bid.hasErrors()
		assert Bid.count() == 0
	}
}
